//
//  JDBaseUpdateImageExtension.swift
//  DragonWallet
//
//  Created by volantPig on 2024/3/12.
//

import UIKit
import SwiftyJSON

class JDBaseUpdateImageExtension: NSObject {

}

extension JDBaseViewController {
    
    /// 上传图片 -- 好用
    @objc func uploadImgV(_ updateImg: UIImage, _ updateCompleted:@escaping ([String: Any]?) -> Void) {
        
        let params = [String: Any]()
        self.showLoading()
        JDNetWorkRequest(.uploadImage(params, [updateImg], "images")) { responseStr in
            
            printLog("图片上传成功:\(responseStr)")
            self.dismissLoading()
            let commonModel = JDCommonModel.deserialize(from: responseStr, designatedPath: "")
            if commonModel?.code == "0" {
                updateCompleted(JSON(commonModel?.data ?? [:]).dictionary)
//                commonModel?.data
            }
            
//            {"msg":"ok","data":{"1722961356.jpg":"9aa67c6e8e72fd72ac97063c01d2e89af6638439abfaa3e221091677fca3fbef"},"code":0}
            
        } failure: { error in
            
            printLog("图片上传错误\(error)")
        } error: { errorCode in
            
            printLog("图片上传失败:\(errorCode)")
        }

        
//        var params = [String: Any]()
//        // TODO: 传userid吧，路径用
//        params["orgid"] = JDLoginModel.default.userId
//
//        JDNetWorkRequest(.uploadImg(params, [updateImg], "file")) { responseStr in
//
//            let commonModel = JDCommonModel.deserialize(from: responseStr, designatedPath: "")
//            if commonModel?.code == "0" {
//                // 成功
//                updateCompleted(JSON(commonModel?.data).stringValue)
//            }
//        } failure: { error in
//            printLog("失败\(error)")
//        } error: { errorCode in
//            printLog("错误\(errorCode)")
//        }
    }
    
    func uploadVideo(_ updateVideoPathUrl: URL, _ updateCompleted:@escaping ([String: Any]?) -> Void) {
        
        let params = [String: Any]()
        
        JDNetWorkRequest(.uploadVideo(params, updateVideoPathUrl, "video")) { responseStr in
            
            let commonModel = JDCommonModel.deserialize(from: responseStr, designatedPath: "")
            if commonModel?.code == "0" {
                updateCompleted(JSON(commonModel?.data ?? [:]).dictionary)
            }
            
            printLog("视频上传成功:\(responseStr)")
//        视频上传成功:{"msg":"ok","data":{".mp4":"783535091c843adf123499e6960c0b38d2bfbf0b1693fa0e82561c0af65c54fa"},"code":0}
            
        } failure: { error in
            printLog("视频上传失败:\(error)")
        } error: { errorCode in
            printLog("视频上传错误:\(errorCode)")
        }

//
//        JDNetWorkRequest(.uploadVideo(params, updateVideoPathUrl, "file")) { responseStr in
//
//            let commonModel = JDCommonModel.deserialize(from: responseStr, designatedPath: "")
//            if commonModel?.code == "0" {
//                // 成功
//                updateCompleted(JSON(commonModel?.data).stringValue)
//            }
//            printLog("上传成功返回的url:\(JSON(commonModel?.data).stringValue)")
//        } failure: { error in
//
//            printLog("失败:\(error)")
//        } error: { errorCode in
//
//            printLog("错误code:\(errorCode)")
//        }
        
    }
    
}
